--[[
    desc: 新版风暴峡湾战斗记录
    author:Hoo
    time:2022-03-07 15:22:03
]]

---@class UIChaosTestRecordNew
local UIChaosTestRecordNew = UIBase.New(PanelResNames.UIChaosTestRecordNew)
local M = UIChaosTestRecordNew

function M.Init()
    UIBase.Init(M)
end

---@protected
function M:OnCreate()
    self:RegisterOnClick(self:GetChild("CloseBg"), self.onClickClose, self)
    self.ctrlChange = self:GetController("change")
    self.listRecord = self:GetChild("RecordList")
    self:SetDynamicList(self.listRecord, self.recordItemRenderer, self)

    self:RegisterEvent(Config.EventType.Trials_Chaos_Record_Info, self.refreshData, self)
end
---@protected
function M:OnEnable()
    TrialHandel.SendTrialsRecordInfo()
end
---@protected
function M:OnDisable()
end
---@protected
function M:OnDestroy()
    self.recordDatas = nil
    self:UnRegisterEvent(Config.EventType.Trials_Chaos_Record_Info, self.refreshData, self)
end

--- 战斗记录数据
---@private
function M:refreshData()
    local recordInfo = logicMgr.TrialsManager.GetTrialsRecordInfo()
    if not recordInfo then return end

    ---@type BattleRecord[] 战斗记录
    self.recordDatas = recordInfo.records

    local count = #self.recordDatas
    self.listRecord.numItems = count
    self.ctrlChange.selectedIndex = count <= 0 and 1 or 0
end

--- 记录战斗列表
---@private
---@param index number  索引
---@param obj   GObject 记录战斗item对象
function M:recordItemRenderer(index, obj)
    local data = self.recordDatas[index]
    if not data then return end

    local txtName = obj:GetChild("PlayerName")
    local txtDmg  = obj:GetChild("TotalDmg")
    local icon    = obj:GetChild("Head"):GetChild("head"):GetChild("HeadIcon")
    local frame   = obj:GetChild("Head"):GetChild("frame")

    -- 计算伤害值
    -- local damage = 0;
    -- for _, v in pairs(data.attacker.memberStats) do
    --     damage = damage + tonumber(tostring(v.attack))
    -- end
    
    txtName.text = data.attacker.playerName
    txtDmg.text  = getNumString(data.attacker.score or 0)
    icon.url     = logicMgr.SkinManager.GetHeroIconBySkinId(tonumber(data.attacker.pic))
    frame.url    = EnumConst.AtlasPath.ICONAtlas .. data.attacker.picFrame

    local recordBtn = obj:GetChild("Record")
    recordBtn.onClick:Clear()
    recordBtn.onClick:Add(function()
        showUI(PanelNames.UILevelRecode, EnumConst.FightTypeConst.TRIAL, data, nil, function ()
            showUI(PanelNames.UIChaosTestRecordNew)
        end)
    end)
end

--- 关闭界面
---@private
function M:onClickClose()
    closeUI(PanelResNames.UIChaosTestRecordNew)
end

return M
